home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1989 / 07 / mcmanis.lst < prev    next >
File List  |  1989-06-01  |  8KB  |  258 lines

  1. _MULTITASKING OS AND GRAPHICS COPROCESSORS_
  2. by Chuck McManis
  3.  
  4.  
  5. [LISTING ONE] 
  6.  
  7. /*    dualvp.c - Dual Viewports on the amiga
  8.  *    Written 4/4/89 by C. McManis using Lattice C 5.02
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <graphics/gfx.h>
  14. #include <graphics/view.h>
  15. #include <graphics/gfxbase.h>
  16. #include <graphics/rastport.h>
  17. extern struct GfxBase *GfxBase;
  18. char    *TextString = "Amiga Graphics Example";
  19.  
  20.             /* Viewport 0 colors */
  21. UWORD            colors0[4] = {0xccc, 0x000, 0x0f0, 0x00f},
  22.             /* Viewport 1 colors */
  23.             colors1[4] = {0x0f0, 0xc0c, 0xf00, 0xfff};
  24. void
  25. _main()
  26. {
  27.     struct View    MyView, *OldView;
  28.     struct ViewPort    Vp0, Vp1;
  29.     struct BitMap    Bits;
  30.     struct RasInfo    MyRaster;
  31.         struct RastPort    rp;
  32.     int        i;
  33.  
  34.     /* Open the resident graphics library */
  35.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L);
  36.     if (!GfxBase) 
  37.         exit(1);
  38.     OldView = GfxBase->ActiView; /* Save this away */
  39.  
  40.     /* Initialize the View structures */
  41.     InitView(&MyView);
  42.     InitVPort(&Vp0);
  43.     InitVPort(&Vp1);
  44.     Vp1.Next = NULL;
  45.     Vp0.Next = &Vp1;    /* create a linked list of viewports */
  46.     MyView.ViewPort = &Vp0;    /* With the first one being Vp0 */
  47.  
  48.     /* Set up some display memory */
  49.     InitBitMap(&Bits, 2, 640, 200); 
  50.     Bits.Planes[0] = (PLANEPTR)
  51.              AllocMem(2*RASSIZE(640, 200),MEMF_CHIP+MEMF_CLEAR);
  52.     Bits.Planes[1] = Bits.Planes[0] + RASSIZE(640, 200);
  53.     if (!Bits.Planes[0]) 
  54.         goto cleanup;
  55.     MyRaster.BitMap = &Bits;
  56.     MyRaster.RxOffset = 0;
  57.     MyRaster.RyOffset = 0;
  58.     MyRaster.Next = NULL;
  59.  
  60.     /* Both viewports are looking at the same display memory but have
  61.          * different sets of colors
  62.      */
  63.     Vp0.RasInfo = &MyRaster;
  64.     Vp0.DWidth  = 320;
  65.     Vp0.DHeight = 175;
  66.     Vp0.ColorMap = (struct ColorMap *)GetColorMap(4);
  67.     LoadRGB4(&Vp0, colors0, 4);
  68.     Vp1.RasInfo = &MyRaster;
  69.     Vp1.DWidth  = 640;
  70.     Vp1.DHeight = 20;
  71.     Vp1.DyOffset = 179;
  72.     Vp1.Modes   = HIRES;
  73.     Vp1.ColorMap = (struct ColorMap *)GetColorMap(4);
  74.     LoadRGB4(&Vp1, colors1, 4);
  75.     
  76.     /* Initialize a RastPort so that we can draw into that memory. */
  77.     InitRastPort(&rp);
  78.     rp.BitMap = &Bits;
  79.     SetAPen(&rp, 1);    /* Foreground color */
  80.     SetBPen(&rp, 0);    /* Background color */
  81.     Move(&rp, 3, 12);    /* Move the graphics cursor to (3, 12) */
  82.     /* Write something */
  83.     Text(&rp, TextString, strlen(TextString));
  84.     MakeVPort(&MyView, &Vp0); /* Build the copper list for Viewport 0 */
  85.     MakeVPort(&MyView, &Vp1); /* Build the copper list for Viewport 1 */
  86.     MrgCop(&MyView);       /* Merge it into the final list */
  87.     LoadView(&MyView);      /* Show it off */
  88.  
  89.     /* SPIN FOR A WHILE */
  90.     for (i=0; i<1000000; i++)
  91.         ;
  92.     LoadView(OldView);       /* Return to the old view */
  93. cleanup:
  94.  
  95.     /* Now give back the memory other tasks may need it */
  96.     if (!Vp0.ColorMap)
  97.         FreeColorMap(Vp0.ColorMap);
  98.     if (!Vp1.ColorMap)
  99.         FreeColorMap(Vp1.ColorMap);
  100.     FreeVPortCopLists(&Vp0);
  101.     FreeVPortCopLists(&Vp1);
  102.     FreeCprList(MyView.LOFCprList);
  103.     if (!Bits.Planes[0]) 
  104.         FreeMem(Bits.Planes[0], 2*RASSIZE(640, 200));
  105.     if (!GfxBase) 
  106.         CloseLibrary(GfxBase);
  107.     exit(0);
  108. }
  109.  
  110.  
  111.  
  112. [LISTING TWO]
  113.  
  114. 000208a8  2b01:fffe    WAIT    (00,2b)        ; (x = 0, y = 43)
  115. --- load color table ---
  116. 000208bc  008e:0581    MOVE    0581,diwstrt    ; (left = 129, top = 5)
  117. 000208c0  0100:0200    MOVE    0200,bplcon0
  118. 000208c4  0104:0024    MOVE    0024,bplcon2
  119. 000208c8  0090:40c1    MOVE    40c1,diwstop    ; (right = 449, bottom = 320)
  120. 000208cc  0092:0038    MOVE    0038,ddfstrt    ; pixel val = 112
  121. 000208d0  0094:00d0    MOVE    00d0,ddfstop    ; pixel val = 416
  122. 000208d4  0102:0000    MOVE    0000,bplcon1
  123. 000208d8  0108:0028    MOVE    0028,bpl1mod
  124. 000208dc  010a:0028    MOVE    0028,bpl2mod
  125. 000208e0  00e0:0002    MOVE    0002,bpl1pth
  126. 000208e4  00e2:86e8    MOVE    86e8,bpl1ptl
  127. 000208e8  00e4:0002    MOVE    0002,bpl2pth
  128. 000208ec  00e6:c568    MOVE    c568,bpl2ptl
  129. 000208f0  2c01:fffe    WAIT    (00,2c)        ; (x = 0, y = 44)
  130. 000208f4  0100:2200    MOVE    2200,bplcon0
  131. 000208f8  db01:fffe    WAIT    (00,db)        ; (x = 0, y = 219)
  132. 000208fc  0100:0200    MOVE    0200,bplcon0
  133. 00020900  de01:fffe    WAIT    (00,de)        ; (x = 0, y = 222)
  134. --- load color table ---
  135. 00020914  008e:0581    MOVE    0581,diwstrt    ; (left = 129, top = 5)
  136. 00020918  0100:0200    MOVE    0200,bplcon0
  137. 0002091c  0104:0024    MOVE    0024,bplcon2
  138. 00020920  0090:40c1    MOVE    40c1,diwstop    ; (right = 449, bottom = 320)
  139. 00020924  0092:003c    MOVE    003c,ddfstrt    ; pixel val = 120
  140. 00020928  0094:00d0    MOVE    00d0,ddfstop    ; pixel val = 416
  141. 0002092c  0102:0000    MOVE    0000,bplcon1
  142. 00020930  0108:0000    MOVE    0000,bpl1mod
  143. 00020934  010a:0000    MOVE    0000,bpl2mod
  144. 00020938  00e0:0002    MOVE    0002,bpl1pth
  145. 0002093c  00e2:86e8    MOVE    86e8,bpl1ptl
  146. 00020940  00e4:0002    MOVE    0002,bpl2pth
  147. 00020944  00e6:c568    MOVE    c568,bpl2ptl
  148. 00020948  df01:fffe    WAIT    (00,df)        ; (x = 0, y = 223)
  149. 0002094c  0100:a200    MOVE    a200,bplcon0
  150. 00020950  f301:fffe    WAIT    (00,f3)        ; (x = 0, y = 243)
  151. 00020954  0100:0200    MOVE    0200,bplcon0
  152. 00020958  ffff:fffe    WAIT    (7f,ff)        ; (x = 127, y = 255)
  153.  
  154.  
  155.  
  156. [LISTING THREE]
  157.  
  158. /*    blit.c - Demonstrates the benefit of the blitter.
  159.  *    Written 4/9/89 by C. McManis using Lattice C 5.02
  160.  *    The difference on my machine between waiting for the blitter
  161.  *    to complete before calculating the next set of draw parameters
  162.  *    is 1.6 vs 1.4 seconds, about a 12.5% increase in speed.
  163.  */
  164.  
  165. #include <exec/types.h>
  166. #include <exec/memory.h>
  167. #include <intuition/intuition.h>
  168. #include <graphics/gfx.h>
  169. extern struct IntuitionBase *IntuitionBase;
  170. extern struct GfxBase        *GfxBase;
  171. struct NewScreen NS = {
  172.         0, 0,        /* Position on the display          */
  173.         320, 200, 4,    /* Attributes (Width, Height, Depth) */
  174.         1,0,        /* Detail and Block pens         */
  175.         0,        /* ViewModes nothing special         */
  176.         CUSTOMSCREEN,    /* It is our own screen we want         */
  177.         0,        /* Using the Default font         */
  178.         "Blitter Test",    /* With a simple title.             */
  179.         0,        /* No special gadgets             */
  180.         0        /* And no special bitmap         */
  181.     };
  182. struct Screen    *MyScreen;
  183. struct RastPort    *RPort;
  184. void
  185. cleanup(n)
  186.     int    n;
  187. {
  188.     if (GfxBase) 
  189.         CloseLibrary(GfxBase);
  190.     if (MyScreen)
  191.         CloseScreen(MyScreen);
  192.     if (IntuitionBase)
  193.         CloseLibrary(IntuitionBase);
  194.     exit(n);
  195. }
  196. void
  197. main()
  198. {
  199.     int    i,     /* Loop counter */
  200.         x, y, c, /* some random draw parameters */
  201.         t0[2],     /* Start Time */
  202.         t1[2];     /* End time */
  203.     IntuitionBase = (struct IntuitionBase *)
  204.                 OpenLibrary("intuition.library",0L);
  205.     if (! IntuitionBase) 
  206.         cleanup(1);
  207.     GfxBase = (struct GfxBase *)
  208.                 OpenLibrary("graphics.library", 0L);
  209.     if (! GfxBase)
  210.         cleanup(1);
  211.  
  212.     /* This does all of the view construction for us */
  213.     MyScreen = (struct Screen *) OpenScreen(&NS);
  214.     if (!MyScreen) 
  215.         cleanup(1);
  216.     timer(t0);    /* Start the clock running */
  217.  
  218.     /* Get the RastPort of this screen */
  219.     RPort = &(MyScreen->RastPort);
  220.     SetAPen(RPort, 1);        /* Foreground pen = 1 */
  221.     SetBPen(RPort, 0);        /* Background pen = 0 */
  222.     srand(42);            /* set the seed */
  223.     Move(RPort, 160, 100);         /* Move to the moiddle of the screen */  
  224.  
  225.     /*
  226.      * Note we generate psuedo random numbers (eg the same set of 
  227.       * random numbers every time.
  228.      */
  229.     for (i=0; i<1000; i++) {
  230.         x = (rand() % 300) + 10;
  231.         y = (rand() % 180) + 10;
  232.         c = rand() % 16;
  233.         SetAPen(RPort, c);
  234.         Draw(RPort, x, y);
  235. #ifdef WAIT_BLIT
  236.         WaitBlit();    /* Simulate non-coprocessor */
  237. #endif
  238.     }
  239.     timer(t1);    /* stop the clock */
  240. #ifdef WAIT_BLIT
  241.     printf("With waiting for the blitter, we took %d microseconds.\n",
  242.             (t1[0] - t0[0]) * 1000000 + (t1[1] - t0[1]));
  243. #else
  244.     printf("Without waiting for the blitter, we took %d microseconds.\n",
  245.             (t1[0] - t0[0]) * 1000000 + (t1[1] - t0[1]));
  246. #endif        
  247.     cleanup(0);
  248. }
  249.  
  250.  
  251.  
  252. Example 1. Sample Copper code
  253.  
  254.     WAIT    0,261         ; Wait for line 261
  255.     MOVE    COPR, INTREQ ; Interrupt the CPU
  256.     WAIT    0,$FFFE         ; Wait until VBlank
  257.  
  258.